package com.cskaoyan.controller.configmanage;

import com.cskaoyan.module.bean.BaseRespVo;

import com.cskaoyan.module.bean.configmanageBo.AdminConfigMall4PostBO;
import com.cskaoyan.module.bean.configmanageBo.AdminConfigOrderBo;
import com.cskaoyan.module.bean.configmanageBo.AdminConfigWxBo;
import com.cskaoyan.module.bean.configmanageBo.ShippingRateConfigurationBO;
import com.cskaoyan.service.configmanage.MarketConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/config")
public class MarketConfig {
    @Autowired
    MarketConfigService marketConfigService;

    @GetMapping("mall")
    @ResponseBody
    public BaseRespVo mall4Get() {

        return marketConfigService.getInitInformation();


    }

    /**
     * 参数key：
     * {
     * "market_mall_longitude":"31°22'",
     * "market_mall_latitude":"115°05'",
     * "market_mall_address":"武汉",
     * "market_mall_qq":"45151845",
     * "market_mall_phone":"6541651465",
     * "market_mall_name":"15155"
     * }
     *
     * @param
     * @return
     */
    @PostMapping("mall")
    @ResponseBody
    @Validated
    public BaseRespVo mall4Post(@RequestBody AdminConfigMall4PostBO adminConfigMall4PostBO, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {//如果有校验错误，才做校验结果处理
            //拿到参数错误
            return parameterValidation(bindingResult);
        }


        return marketConfigService.upDataInfomation(adminConfigMall4PostBO);
    }


    private BaseRespVo parameterValidation(BindingResult bindingResult) {
        FieldError fieldError = bindingResult.getFieldError();
        //拿到请求参数名
        String field = fieldError.getField();
        //传入的错误的值
        Object rejectedValue = fieldError.getRejectedValue();
        String defaultMessage = fieldError.getDefaultMessage();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("请求参数").append(field).
                append("因为传入了").append(rejectedValue).
                append("导致异常").append(defaultMessage);
        return BaseRespVo.invalidData(stringBuffer.toString());
    }


    //http://182.92.235.201:8083/admin/config/express
    @GetMapping("express")
    @ResponseBody
    public BaseRespVo shippingRateConfiguration4Get() {


        Integer[] Ids = new Integer[]{6,8};
        return marketConfigService.getRate(Ids);
    }

    @PostMapping("express")
    @ResponseBody
    @Validated
    public BaseRespVo shippingRateConfiguration4Post(@RequestBody ShippingRateConfigurationBO shippingRateConfigurationBO, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {//如果有校验错误，才做校验结果处理
            //拿到参数错误
            return parameterValidation(bindingResult);
        }
        return marketConfigService.upShippingRate(shippingRateConfigurationBO);

    }

    //GET http://182.92.235.201:8083/admin/config/order HTTP/1.1
    @GetMapping("order")
    @ResponseBody
    public BaseRespVo order4Get() {
               Integer[] Ids = new Integer[]{1,4,11};
        return marketConfigService.getOrderInformation(Ids);


    }

    //POST http://182.92.235.201:8083/admin/config/order HTTP/1.1
    @PostMapping("order")
    @ResponseBody
    public BaseRespVo order4Post(@RequestBody AdminConfigOrderBo adminConfigOrderBo, BindingResult bindingResult){
        if (bindingResult.hasFieldErrors()) {//如果有校验错误，才做校验结果处理
            //拿到参数错误
            return parameterValidation(bindingResult);
        }
        return  marketConfigService.upOrder(adminConfigOrderBo);

    }
    //GET http://182.92.235.201:8083/admin/config/wx HTTP/1.1
    @GetMapping("wx")
    @ResponseBody
    public BaseRespVo wx4Get(){
        AdminConfigWxBo adminConfigWxBo = new AdminConfigWxBo();
//        adminConfigWxBo.setMarketWxCatlogGoods("四");
//        adminConfigWxBo.setMarketWxCatlogList("2");
//        adminConfigWxBo.setMarketWxIndexBrand("1");
//        adminConfigWxBo.setMarketWxIndexHot("1");
//        adminConfigWxBo.setMarketWxIndexNew("1");
//        adminConfigWxBo.setMarketWxIndexTopic("2");
//        adminConfigWxBo.setMarketWxShare("false");
        Integer[] Ids = new Integer[]{2,5,10,12,15,17,18};
        return marketConfigService.getWxInformation(Ids);

    }
    //POST http://182.92.235.201:8083/admin/config/wx HTTP/1.1
    @PostMapping("wx")
    @ResponseBody
    @Validated
    public BaseRespVo we4Post(@RequestBody AdminConfigWxBo adminConfigWxBo,BindingResult bindingResult){
        if (bindingResult.hasFieldErrors()) {//如果有校验错误，才做校验结果处理
            //拿到参数错误
            return parameterValidation(bindingResult);
        }
        return  marketConfigService.upWx(adminConfigWxBo);

    }



}